// miniprogram/pages/player/player.js
let musiclist = [];
// 正在播放的INDEx
let nowPlayingIndex = 0;
const backgroundAudioManager = wx.getBackgroundAudioManager();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: "",
    isPlaying: false, //false表示不播放
    isLyricShow: false, //是否显示歌词
    lyric: "",
    isSame: false, //是否为同一首歌
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, "this is options");
    musiclist = wx.getStorageSync("musiclist");
    nowPlayingIndex = options.index;
    this._loadMusicDetail(options.musicId);
  },

  _loadMusicDetail(musicId) {
    if (musicId == app.getPlayMusicId()) {
      this.setData({
        isSame: true
      })
    } else {
      this.setData({
        isSame: false
      })
    }
    if (!this.data.isSame) {
      backgroundAudioManager.stop();
    }
    let music = musiclist[nowPlayingIndex];
    wx.setNavigationBarTitle({
      title: music.name
    })
    this.setData({
      picUrl: music.al.picUrl,
      isPlaying: false
    })
    app.setPlayMusicId(musicId);
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'music',
      data: {
        musicId,
        $url: "musicUrl"
      }
    }).then(res => {
      let result = JSON.parse(res.result);
      if (result.data[0].url == null) {
        wx.showToast({
          title: '无权限播放',
        })
        return;
      }
      if (!this.data.isSame) {
        backgroundAudioManager.src = result.data[0].url;
        backgroundAudioManager.title = music;
        backgroundAudioManager.coverImgUrl = music.picUrl;
        backgroundAudioManager.singer = music.ar[0].name;
        backgroundAudioManager.epname = music.al.name;
        this.savePlayHistory();
      }

      this.setData({
        isPlaying: true
      })
      wx.hideLoading();
      // 下载歌词
      wx.cloud.callFunction({
        name: 'music',
        data: {
          $url: "lyric",
          musicId
        }
      }).then((res) => {
        console.log("lyric", res);
        let lyric = '暂无歌词';
        const lyc = res.result.lrc;
        if (lyc) {
          lyric = lyc.lyric;
        }
        this.setData({
          lyric
        })
      })
    })

  },

  togglePlaying() {
    // 正在播放
    if (this.data.isPlaying) {
      backgroundAudioManager.pause();
    } else {
      backgroundAudioManager.play();
    }
    this.setData({
      isPlaying: !this.data.isPlaying
    })
  },

  onPrev() {
    nowPlayingIndex--
    if (nowPlayingIndex < 0) {
      nowPlayingIndex = musiclist.length - 1;
    }
    this._loadMusicDetail(musiclist[nowPlayingIndex].id);
  },
  onNext() {
    nowPlayingIndex++
    if (nowPlayingIndex === musiclist.length) {
      nowPlayingIndex = 0;
    }
    this._loadMusicDetail(musiclist[nowPlayingIndex].id);
  },

  onChangeLyricShow() {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },

  timeUpdate(event) {
    this.selectComponent('.lyric1').update(event.detail.currentTime)
  },

  musicPlay() {
    this.setData({
      isPlaying: true
    })
  },
  musicPause() {
    this.setData({
      isPlaying: false
    })
  },

  savePlayHistory() {
    const music = musiclist[nowPlayingIndex];
    const openid = app.globalData.openid;
    console.log(openid,"asdads")
    const history = wx.getStorageSync(openid);
    let bHave = false;
    for(let i = 0;i<history.length;i++){
      if(history[i].id == music.id) {
        bHave = true;
        break
      }
    }
    if(!bHave) {
      history.unshift(music);
      wx.setStorage({
        data: history,
        key: openid,
      })
    }


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})